﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace YXShop.Web.admin.store
{
    public partial class store_edit : System.Web.UI.Page
    {
        YXShop.BLL.Store.Store storeBll = new YXShop.BLL.Store.Store();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");
                if (id > 0)
                {
                    YXShop.Common.PromptInfo.Popedom("014001004", "对不起，您没有权限进行修改");
                    lblOperator.Text = "编辑";
                }
                else
                {
                    YXShop.Common.PromptInfo.Popedom("014001002", "对不起，您没有权限进行新增");
                    lblOperator.Text = "新增";
                }
                InitWebControl();
                if (id > 0)
                {
                    GetStore(id);
                }
            }
        }

        private void GetStore(int id)
        {
            YXShop.Model.Store.Store s = new Model.Store.Store();
            YXShop.BLL.Admin.Administrators admin = new BLL.Admin.Administrators();
            YXShop.BLL.Store.StoreCenter scBLL = new BLL.Store.StoreCenter();
            s = storeBll.GetModelById(id);
            txtStoreName.Text = s.Storename;
            hfadminusername.Value = s.Adminid.ToString();
            txtAdminUserName.Text = admin.GetModel(s.Adminid.Value).Name;
            hfstoreid.Value = s.Parentstoreid.Value.ToString();
            if (s.Parentstoreid.Value > 0)
                txtParentStoreName.Text = storeBll.GetModelById(s.Parentstoreid.Value).Storename;
            hfStoreCenterid.Value = s.Storecenterid.Value.ToString();
            txtStoreCenter.Text = scBLL.GetModelById(s.Storecenterid.Value).StoreCentername;
            for (int i = 0; i < ddlType.Items.Count; i++)
            {
                if (ddlType.Items[i].Value == s.Storetype.ToString())
                    ddlType.Items[i].Selected = true;
            }
            txtStartDate.Text = s.Startdate.Value.ToString("yyyy-MM-dd");
            if (s.Enddate.HasValue)
                txtEndDate.Text = s.Enddate.Value.ToString("yyyy-MM-dd");
            lblScript.Text += "GetCity('" + this.ddlProvince.ClientID + "','0','" + s.Province + "');\n";
            lblScript.Text += "GetCity('" + this.ddlCity.ClientID + "','" + s.Province + "','" + s.City + "');\n";
            lblScript.Text += "GetCity('" + this.ddlBorough.ClientID + "','" + s.City + "','" + s.Borough + "');\n";
            txtAddress.Text = s.Address;
            txtZipCode.Text = s.Zipcode;
            txtMobilePhone.Text = s.Mobilephone;
            txtPhone.Text = s.Phone;
            txtStoreContent.Value = s.Discription;
            txtContact.Text = s.Contact;
            txtFax.Text = s.Fax;
        }

        #region 验证
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.WebControl.Validate(this.txtStoreName, "门店名称不能为空", "isnull", "必填", "该项为必填项");
            this.txtAdminUserName.Attributes.Add("readonly", "readonly");
            this.txtAdminUserName.Attributes.Add("onclick", "selectFile('Adminlist',new Array(" + this.hfadminusername.ClientID + "," + this.txtAdminUserName.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            this.txtParentStoreName.Attributes.Add("readonly", "readonly");
            this.txtParentStoreName.Attributes.Add("onclick", "selectFile('Storelist',new Array(" + this.hfstoreid.ClientID + "," + this.txtParentStoreName.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            this.txtStoreCenter.Attributes.Add("readonly", "readonly");
            this.txtStoreCenter.Attributes.Add("onclick", "selectFile('Storecenterlist',new Array(" + this.hfStoreCenterid.ClientID + "," + this.txtStoreCenter.ClientID + "),310,450,'" + sp.DummyPaht + "');");

            ChangeHope.WebPage.WebControl.Validate(this.txtAdminUserName, "管理员不能为空", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtStoreCenter, "销售中心不能为空", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtStartDate, "开业日期不能为空", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtZipCode, "邮政编码", "ispostalcode", "需符合邮政编码格式", "该项需符合邮政编码格式");
            ChangeHope.WebPage.WebControl.Validate(this.txtFax, "传真", "isfaxorphone", "需符合传真号码格式", "该项需符合传真号码格式");
            ChangeHope.WebPage.WebControl.Validate(this.txtMobilePhone, "手机号码", "ismobilephone", "需符合手机号码格式", "该项需符合手机号码格式");
            ChangeHope.WebPage.WebControl.Validate(this.txtPhone, "电话", "isfaxorphone", "需符合电话号码格式", "该项需符合电话号码格式");
            ChangeHope.WebPage.WebControl.SetDate(this.txtStartDate);
            ChangeHope.WebPage.WebControl.SetDate(this.txtEndDate);

           
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
            BindType();

            this.ddlProvince.Attributes.Add("onchange", "GetCity('" + this.ddlCity.ClientID + "',this.value);");
            this.ddlCity.Attributes.Add("onchange", "GetCity('" + this.ddlBorough.ClientID + "',this.value);");
            this.lblScript.Text = "GetCity('" + this.ddlProvince.ClientID + "','0');\n";
            //if (!sp.IsCloseStation)
            //{
            //    this.txtArea.Enabled = false;
            //}
        }
        #endregion

        #region 类型
        private void BindType()
        {
            this.ddlType.DataSource = YXShop.Common.ValuePair.GetStoreType();
            this.ddlType.DataTextField = "StoreTypeName";
            this.ddlType.DataValueField = "StoreTypeID";
            this.ddlType.DataBind();
        }
        #endregion

        #region 保存数据
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }


        private void Save()
        {
            int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");

            YXShop.BLL.Admin.Administrators adminBll = new YXShop.BLL.Admin.Administrators();
            YXShop.Model.Store.Store store = new YXShop.Model.Store.Store();
            string dd = this.hfadminusername.Value.ToString();
            YXShop.Model.Admin.Administrators admin = adminBll.GetModel(Convert.ToInt32(dd));
            if (admin == null)
            {
                this.ltlMsg.Text = "操作失败,用户名有误";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }

            List<YXShop.Model.Store.Store> existStore = storeBll.GetListShopByWhere("and storename='" + txtStoreName.Text + "'");
            if (id > 0)
            {
                if (existStore.Count > 0 && existStore[0].Sid.Value != id)
                {
                    ltlMsg.Text = "操作失败,门店名称重复";
                    pnlMsg.Visible = true;
                    pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            else
            {
                if (existStore.Count > 0)
                {
                    ltlMsg.Text = "操作失败,门店名称重复";
                    pnlMsg.Visible = true;
                    pnlMsg.CssClass = "actionErr";
                    return;
                }
                
            }

            store.Storename = txtStoreName.Text;
            store.Adminid = admin.AdminId;
            if(!string.IsNullOrEmpty(hfstoreid.Value))
                store.Parentstoreid = Convert.ToInt32(hfstoreid.Value);
            else
                store.Parentstoreid=0;
            store.State = 1;
            store.Storecenterid = Convert.ToInt32(hfStoreCenterid.Value);
            store.Storetype = Convert.ToInt32(ddlType.SelectedValue);
            store.Discription = txtStoreContent.Value;
            store.Startdate = Convert.ToDateTime(txtStartDate.Text);
            if (string.IsNullOrEmpty(txtEndDate.Text))
                store.Enddate = DateTime.MaxValue;
            else
                store.Enddate = Convert.ToDateTime(txtEndDate.Text);
            store.Addtime = DateTime.Now;
            store.Updatetime = DateTime.Now;
            store.Province = ChangeHope.WebPage.PageRequest.GetFormString(ddlProvince.UniqueID);
            store.City = ChangeHope.WebPage.PageRequest.GetFormString(ddlCity.UniqueID);
            store.Borough = ChangeHope.WebPage.PageRequest.GetFormString(ddlBorough.UniqueID);
            store.Address = txtAddress.Text;
            store.Zipcode = txtZipCode.Text;
            store.Mobilephone = txtMobilePhone.Text;
            store.Phone = txtPhone.Text;
            store.Contact = txtContact.Text;
            store.Fax = txtFax.Text;



            int count = 0;
            if (id > 0)
            {
                store.Sid = id;
                count = storeBll.Update(store);
            }
            else
            {
                count = storeBll.Add(store);
            }
            if (count > 0)
            {
                if (id > 0)
                {
                    this.ltlMsg.Text = "操作成功，已修改该信息";
                    Response.Redirect("store_edit.aspx?id=" + id.ToString());
                }
                else
                {
                    this.ltlMsg.Text = "操作成功，已添加该信息";
                    Response.Redirect("store_list.aspx");
                }

                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
            else
            {
                this.ltlMsg.Text = "操作失败";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
            }

        }
        #endregion
    }
}
